package com.itheima.health.service.impl;

import com.itheima.health.dao.OrderSettingMapper;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@DubboService
@Slf4j
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Override
    public List<OrderSetting> findByMonth(Integer year, Integer month) {
        log.info("预约设置-查询所有预约传参service层传参：year：{} ，month ：{}",year,month);
        Example example = new Example(OrderSetting.class);
        Example.Criteria criteria = example.createCriteria();
        //拼接字符串 每月1号
        String beginDate = String.format("%d-%d-01",year,month);
        //拼接字符串 每月31号
        String endDate = String.format("%d-%d-31",year,month);
        //设置查询条件 大于等于 每月一号
        criteria.andGreaterThanOrEqualTo("orderDate",beginDate);
        //设置查询条件 小于等于 每月一号
        criteria.andLessThanOrEqualTo("orderDate",endDate);
        log.info("预约设置-查询所有预约拼接结果：year：{} ，month ：{}",year,month);
        return orderSettingMapper.selectByExample(example);
    }

    /**
     * @Author: 15041
     * @Date: 21:15 2021/1/10
     * @Parms: [orderSetting]
     * @ReturnType: void
     *Description: 设置预约人数
    */
    @Override
    @Transactional //添加事务注解，出现异常回滚
    public void editNumberByDate(OrderSetting orderSetting) {
        log.info("预约设置-设置预约人数传参service层传参：{}",orderSetting);
        Example example = new Example(OrderSetting.class);
        Example.Criteria criteria = example.createCriteria();
        //存在就修改 根据日期查询
        //根据日期查询
        criteria.andEqualTo("orderDate",orderSetting.getOrderDate());
        //如果有就根据传过来的数据修改
        int i = orderSettingMapper.updateByExampleSelective(orderSetting, example);
        log.info("根据日期更新 返回来的结果是：{}",i);
        //不存在就新增
        if (i==0){ //如果i=0说明没有该orderDate中的数据
            //添加
            orderSettingMapper.insertSelective(orderSetting);
        }
    }
    /**
     * @Author: 15041
     * @Date: 22:00 2021/1/10
     * @Parms: [list]
     * @ReturnType: void
     *Description: 批量上传设置预约人数
    */
    @Override
    public void add(List<OrderSetting> list) {
        for (OrderSetting orderSetting : list) {
            orderSettingMapper.insertSelective(orderSetting);
        }
    }
}










